
typedef struct {
    int * arr;
    int front;     //队头
    int rear;      //队尾
    int size;      //队成员数
    int capacity;   //容量
} MyCircularQueue;

//构造器，设置队列长度为 k 
MyCircularQueue* myCircularQueueCreate(int k) {
    //申请结构空间
    MyCircularQueue* queue = (MyCircularQueue*)malloc(sizeof(MyCircularQueue));
    if(NULL == queue){
        return NULL;
    }
    queue->arr = (int*)malloc(sizeof(int)*k);
    if(NULL == queue->arr){
        free(queue);   //如果申请空间失败，释放结构体
        return NULL;
    }
    queue->capacity = k;
    queue->front = 0;
    queue->rear = 0;
    queue->size = 0;
    return queue;
}


//检查循环队列是否已满。
bool myCircularQueueIsFull(MyCircularQueue* obj) {
    return obj->size == obj->capacity;
}


//检查循环队列是否为空。
bool myCircularQueueIsEmpty(MyCircularQueue* obj) {
    return 0 == obj->size;
}

//入队：向循环队列插入一个元素。如果成功插入则返回真。
bool myCircularQueueEnQueue(MyCircularQueue* obj, int value) {
    if(myCircularQueueIsFull(obj)){     //如果队满
        return false;
    }
    obj->arr[obj->rear] = value;
    obj->rear++;
    if(obj->rear == obj->capacity){   //队尾到了空间末尾
        obj->rear = 0;
    }
    //obj->rear = (obj->rear+1)%(obj->capacity);
    obj->size++;
    return true;
}

//出队：从循环队列中删除一个元素。如果成功删除则返回真
bool myCircularQueueDeQueue(MyCircularQueue* obj) {
    if(myCircularQueueIsEmpty(obj)){   //如果队列为空
        return false;
    }
    obj->front++;
    obj->front %= obj->capacity;
    obj->size--;
    return true;
}

//从队首获取元素。如果队列为空，返回 -1 
int myCircularQueueFront(MyCircularQueue* obj) {
    if(myCircularQueueIsEmpty(obj)){   //如果队列为空
        return -1;
    }
    return obj->arr[obj->front];
}

//获取队尾元素。如果队列为空，返回 -1 
int myCircularQueueRear(MyCircularQueue* obj) {
    if(myCircularQueueIsEmpty(obj)){   //如果队列为空
        return -1;
    }
    int n = obj->capacity;
    return obj->arr[(obj->rear - 1 + n) % n];
}

//销毁队列
void myCircularQueueFree(MyCircularQueue* obj) {
    if(obj){
        if(obj->arr){   //如果队列不为空
            free(obj->arr);
            obj->arr = NULL;
            obj->front = obj->rear = 0;
            obj->size = obj->capacity = 0;
        }
        free(obj);
    }
}
/**
 * Your MyCircularQueue struct will be instantiated and called as such:
 * MyCircularQueue* obj = myCircularQueueCreate(k);
 * bool param_1 = myCircularQueueEnQueue(obj, value);
 
 * bool param_2 = myCircularQueueDeQueue(obj);
 
 * int param_3 = myCircularQueueFront(obj);
 
 * int param_4 = myCircularQueueRear(obj);
 
 * bool param_5 = myCircularQueueIsEmpty(obj);
 
 * bool param_6 = myCircularQueueIsFull(obj);
 
 * myCircularQueueFree(obj);
*/